#include "formstatus.h"
#include <QDateTime>
#include <QQuickItem>
#include <QGraphsTheme>
#include "ui_formstatus.h"
FormStatus::FormStatus(QWidget *parent)
	: QWidget(parent)
	, ui(new Ui::FormStatus)
	, m_watchModule(new WatchMemModule(this))
{
	ui->setupUi(this);
	//Mem watch
	ui->tableView_memstatus->setModel(m_watchModule);
	m_nTimerID = startTimer(1000);
	m_DateAxix = new QDateTimeAxis;
	m_DateAxiy = new QValueAxis;
	m_DateAxiy->setRange(0, 256);
	m_DateAxix->setLabelFormat("mm");

	QList<QObject *> seriesList;
	ui->widget_memchart->setResizeMode(QQuickWidget::SizeRootObjectToView);
	QGraphsTheme *theme = new QGraphsTheme;
	theme->setTheme(QGraphsTheme::Theme::MixSeries);
	ui->widget_memchart->setInitialProperties({{"seriesList", QVariant::fromValue(seriesList)},
											   {"axisX", QVariant::fromValue(m_DateAxix)},
											   {"axisY", QVariant::fromValue(m_DateAxiy)},
											   {"theme", QVariant::fromValue(theme)},
											   {"panStyle", 1},
											   {"zoomAreaEnabled", false}});
	ui->widget_memchart->loadFromModule("QtGraphs", "GraphsView");
}

FormStatus::~FormStatus()
{
	//m_DateAxix->deleteLater();
	//m_DateAxiy->deleteLater();
	delete ui;
}

void FormStatus::update_charts()
{
	if (m_watchModule)
	{
		QDateTime dtm = QDateTime::currentDateTime();
		m_watchModule->update_items();
		const int rows = m_watchModule->rowCount();
		QSet<qint64> abst;
		int max_y = 0;
		QVariant seriesListVariant = ui->widget_memchart->rootObject()->property("seriesList");
		QQmlListProperty<QObject> prop = seriesListVariant.value<QQmlListProperty<QObject>>();
		for (int i = 0; i < rows; ++i)
		{
			const qint64 pid = m_watchModule->data(m_watchModule->index(i, 0)).toLongLong();
			const QString name = m_watchModule->data(m_watchModule->index(i, 1)).toString();
			abst.insert(pid);
			double mem = m_watchModule->data(m_watchModule->index(i, 2)).toDouble();
			if (max_y < mem)
				max_y = mem;
			QLineSeries *ps = nullptr;
			if (m_chart_serials.contains(pid) == false)
			{
				ps = new QLineSeries(this);
				m_chart_serials[pid] = ps;
				ps->setName(QString("%1").arg(pid));
				//ps->setColor(QColor((pid * 12711) % 128+64, (pid * 37) % 128, (pid * 281) % 128));
				ps->setHoverable(true);
				connect(ps,
						&QAbstractSeries::hover,
						[this,pid,name](const QString &, QPointF, QPointF value) -> void {
							ui->label_info->setText(QString("%1:%2=%3")
												  .arg(name)
												  .arg(pid)
												  .arg(value.y()));
						});
				prop.append(&prop, ps);
			}
			else
				ps = m_chart_serials[pid];
			ps->append(dtm.toMSecsSinceEpoch(), mem);
			while (ps->count() >= 1200)
				ps->remove(0);
		}
		//delete
		QList<qint64> oldV = m_chart_serials.keys();
		bool need_refill = false;
		foreach (qint64 u, oldV)
		{
			if (abst.contains(u) == false)
				need_refill = true;
		}
		//refill OML obj list
		if (need_refill)
		{
			prop.clear(&prop);
			m_chart_serials.clear();
		}

		max_y *= 1.5;
		m_DateAxiy->setMax(max_y);
		m_DateAxix->setMin(dtm.addSecs(-600));
		m_DateAxix->setMax(dtm);
	}
}
void FormStatus::timerEvent(QTimerEvent *event)
{
	if (m_nTimerID == event->timerId())
		update_charts();
}
